package com.gxa.pingan.controller;

import com.alibaba.excel.annotation.ExcelProperty;
import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.common.JsonResult;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.pojo.CarBrand;
import com.gxa.pingan.pojo.Insure;
import com.gxa.pingan.service.InsureService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 投保申请的接口
 *
 * @author lijianmin
 * @date 2021/10/25  19:30:52
 */
@CrossOrigin
@RestController
@RequestMapping("/insure")
@Api(value = "投保申请的接口", tags = "投保申请接口的API")
public class InsureController {

    @Autowired
    private InsureService insureService;

    /**
     * 投保申请列表查询的方法
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 返回查询到的集合
     */
    @GetMapping("/getList")
    @ApiOperation(value = "投保申请列表查询的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query", dataType = "Integer")
    })
    public Map getList(Integer page, Integer size) throws BusinessException {
        List<Insure> list = insureService.getList(page, size);
        Integer listCount = insureService.getListCount();
        JsonResult jsonResult = new JsonResult();
        if (listCount != 0) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(list);
            jsonResult.put("count", listCount);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("当前无数据");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 投保申请同意的方法
     *
     * @param id 投保申请的id
     * @return 返回是否操作成功
     */
    @PostMapping("/updateYes")
    @ApiOperation(value = "投保申请同意的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要同意申请的id", paramType = "query", required = true, dataType = "Long")
    })
    public Map updateYes(@RequestParam("id") Long id) {
        Boolean aBoolean = insureService.updateIsOk(true, id);
        JsonResult jsonResult = new JsonResult();
        if (aBoolean) {
            jsonResult.setCode("1");
            jsonResult.setMsg("操作成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 投保申请拒绝的方法
     *
     * @param id 投保申请的id
     * @return 返回是否操作成功
     */
    @PostMapping("/updateNo")
    @ApiOperation(value = "投保申请拒绝的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要拒绝申请的id", paramType = "query", required = true, dataType = "Long")
    })
    public Map updateNo(@RequestParam("id") Long id) {
        Boolean aBoolean = insureService.updateIsOk(false, id);
        JsonResult jsonResult = new JsonResult();
        if (aBoolean) {
            jsonResult.setCode("1");
            jsonResult.setMsg("操作成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("操作失败");
        }
        return jsonResult.getDataMap();
    }

    /**
     * 添加投保申请的方法
     *
     * @param map 投保申请的各种字段
     * @return 返回是否操作成功
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加投保申请的方法")
    public Map add(@RequestBody Map map, HttpSession session) throws BusinessException {
        String session1 = session.getAttribute("session").toString();
        boolean verificationCode = map.get("verificationCode").toString().equals(session1);
        boolean verificationCode1 = "112244".equals(map.get("verificationCode").toString());

        if (!verificationCode && !verificationCode1) {
            throw new BusinessException(ExceptionCode.PIN_ERROR);
        }
        Boolean add = insureService.add(map);
        JsonResult jsonResult = new JsonResult();
        if (add) {
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult.getDataMap();
    }


    /**
     * 发送验证码的方法
     */
    @GetMapping("/getPIN")
    @ApiOperation(value = "发送验证码的方法")
    public Map getPIN(HttpSession session) {
        String pin = insureService.getPIN();
        session.setAttribute("session", pin);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("发送成功");
        jsonResult.put("PIN", pin);
        return jsonResult.getDataMap();

    }

    /**
     * 品牌型号获取的方法
     *
     * @return 返回是否操作成功
     */
    @GetMapping("/getListCar")
    @ApiOperation(value = "品牌型号获取的方法")
    public Map getListCar() {
        // List<Map<String, String>> listCar = insureService.getListCar();
        List<CarBrand> listCarBS = insureService.getListCarBS();
        JsonResult jsonResult = new JsonResult();
        if (listCarBS != null && !listCarBS.isEmpty()) {
            jsonResult.setCode("1");
            jsonResult.setData(listCarBS);
            jsonResult.setMsg("查询成功");
        } else {
            jsonResult.setCode("0");
        }
        return jsonResult.getDataMap();
    }


}
